<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1200" :zIndex="999" destroy-on-close @closed="$emit('closed')"
		top="0">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row>
				<el-col :span="11">
					<el-form-item label="标题" prop="title">
						<el-input v-model="form.title" placeholder="" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="1"></el-col>
				<el-col :span="12">
					<el-form-item label="别名" prop="alias">
						<el-input v-model="form.alias" placeholder="" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="11">
					<el-form-item label="分类" prop="category_id">
						<el-cascader v-model="form.category_id" :options="groups" :props="groupsProps" :show-all-levels="false"
									 clearable style="width: 100%;"></el-cascader>
					</el-form-item>
				</el-col>
				<el-col :span="1"></el-col>
				<el-col :span="12">
					<el-form-item label="跳转URL" prop="jump_url">
						<el-input v-model="form.jump_url" placeholder="" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="11">
					<el-form-item label="封面" prop="cover">
						<sc-upload-multiple ref="cover" v-model="form.cover" title="选择封面"></sc-upload-multiple>
					</el-form-item>
				</el-col>
				<el-col :span="1"></el-col>
				<el-col :span="12">
					<el-form-item label="简介" prop="description">
						<el-input v-model="form.description" type="textarea" rows="4" placeholder="" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="文章详情" prop="content">
				<sc-editor v-model="form.content" placeholder="请输入" :height="400" style="width: 100%;"></sc-editor>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {defineAsyncComponent} from "vue";
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增文章',
					edit: '编辑文章',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				baseUrl: '',
				//表单数据
				form: {
					id:0,
					category_id: 0,
					title: '',
					alias: '',
					cover: '',
					description: '',
					content: '',
					jump_url: '',
				},
				//验证规则
				rules: {
					title: [
						{required: true, message: '请输入标题'}
					],
					alias: [
						{required: true, message: '请输入别名'}
					],
					category_id: [
						{required: true, message: '请选择所属分类'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: 'cat_name',
					checkStrictly: true
				}
			}
		},
		mounted() {
			this.getGroup()
			this.getBaseUrl()
		},
		methods: {
			async getBaseUrl() {
				this.baseUrl = await this.$TOOL.getBaseUrl(this.$API);
				setTimeout(() => {
					this.$refs.cover.setBaseUrl(this.baseUrl);
				}, 500)
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			// 获取单条信息
			async getInfo(id) {
				this.isSaveing = true;
				var res = await this.$API.cms.article.info({id: id});
				this.isSaveing = false;
				if(res.code === 0){
					this.form = res.data;
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//加载树数据
			async getGroup(){
				var res = await this.$API.cms.category.list({type:1})
				this.groups = this.$TOOL.deepTree(res.data, 'parent_id');
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let method = this.mode === 'add' ? 'add' : 'update';
						if (this.$TOOL.isArray(this.form.category_id)) {
							this.form.category_id = this.form.category_id[this.form.category_id.length - 1];
						}
						var res = await this.$API.cms.article[method](this.form);
						this.isSaveing = false;
						if(res.code === 0){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				if (this.mode !== 'add') {
					this.getInfo(data.id)
				}
			}
		}
	}
</script>

<style>
</style>
